/*
 * PKL 51 GUNUNG KIDUL
 * Subseksie DATA ENTRY + CAPI
 */
package pkl51.model;

import java.io.Serializable;
import pkl51.entity.*;
import pkl51.error.EntityException;
import pkl51.model.event.Blok123Listener;
import pkl51.service.*;
import pkl51.service.impl.*;

/**
 *
 * @author Hindarwan
 */
public class Blok123Model implements Serializable{

    private static Blok123Model instance;
    private static final long serialVersionUID = 8532063507793674126L;
    private Blok123Listener listener;
    private MahasiswaModel mahasiswaModel; //dibutuhkan untuk mengisi field nim_pengentri di tabel ruta
    //blok 1
    private String nama_kec;
    private String B1R2;
    private String nama_desa;
    private String B1R3;
    private String B1R4;
    private String B1R5;
    private String B1R6;
    private String NKS;
    private String B1R8;
//    private String B1R9; //GAK MASUK DB?
    //blok 2
    private String B2R1;
    private String B2R2;
    private String B2R3;
    //blok3
    private String nama_pencacah; //gak masuk db
    private String B3R1; //nim_pencacah
    private String B3R2a;
    private String B3R2b = "03";
    private String nama_kortim; //gak masuk db
    private String B3R4; //nim_kortim
    private String B3R5a;
    private String B3R5b = "03";

    //
    public static Blok123Model getInstance() {
        if (instance == null) {
            instance = new Blok123Model();
        }
        return instance;
    }

    private Blok123Model() {
    }

    //Yang ini cuma setter utk ngisi model (khusus yg mau dimasukkan ke db)
    public void setForDB(String B1R2, String B1R3, String B1R4, String B1R5, String B1R6, String NKS, String B1R8, String B2R1, String B2R2, String B2R3, String B3R1, String B3R2a, String B3R2b, String B3R4, String B3R5a, String B3R5b) {
        this.B1R2 = B1R2;
        this.B1R3 = B1R3;
        this.B1R4 = B1R4;
        this.B1R5 = B1R5;
        this.B1R6 = B1R6;
        this.NKS = NKS;
        this.B1R8 = B1R8;
        this.B2R1 = B2R1;
        this.B2R2 = B2R2;
        this.B2R3 = B2R3;
        this.B3R1 = B3R1;
        this.B3R2a = B3R2a;
        this.B3R2b = B3R2b;
        this.B3R4 = B3R4;
        this.B3R5a = B3R5a;
        this.B3R5b = B3R5b;
    }

    /*
     * operasi2 yang dilakukan
     */
    //3 operasi utama, insert, upadate, get
    public void insertBlok1() throws EntityException {
        RutaDao rutaDao = new RutaDaoImpl();
        rutaDao.insertRuta(new Ruta(NKS, B1R2, B1R3, B1R4, B1R5, B1R6, B1R8, mahasiswaModel.getNim()));
    }

    public void insertBlok2() throws EntityException {
        Blok2Dao blok2Dao = new Blok2DaoImpl();
        blok2Dao.insertBlok2(new Blok2(NKS, Integer.valueOf(B2R1), Integer.valueOf(B2R2), B2R3));
    }

    public void insertBlok3() throws EntityException {
        Blok3Dao blok3Dao = new Blok3DaoImpl();
        blok3Dao.insertBlok3(new Blok3(NKS, B3R1, B3R2a, B3R2b, B3R4, B3R5a, B3R5b));
    }

    public void updateBlok1() throws EntityException {
        RutaDao rutaDao = new RutaDaoImpl();
        rutaDao.updateRuta(new Ruta(NKS, B1R2, B1R3, B1R4, B1R5, B1R6, B1R8, mahasiswaModel.getNim()));
    }

    public void updateBlok2() throws EntityException {
        Blok2Dao blok2Dao = new Blok2DaoImpl();
        blok2Dao.updateBlok2(new Blok2(NKS, Integer.valueOf(B2R1), Integer.valueOf(B2R2), B2R3));
    }

    public void updateBlok3() throws EntityException {
        Blok3Dao blok3Dao = new Blok3DaoImpl();
        blok3Dao.updateBlok3(new Blok3(NKS, B3R1, B3R2a, B3R2b, B3R4, B3R5a, B3R5b));
    }

    public void getBlok123() throws EntityException {

        RutaDao rutaDao = new RutaDaoImpl();
        Ruta ruta = rutaDao.getRutaByNKS(NKS);
        setB1R2(ruta.getB1R2());
        getValidasiB1R2();
        setB1R3(ruta.getB1R3());
        getValidasiB1R3();
        setB1R4(ruta.getB1R4());
        setB1R5(ruta.getB1R5());
        setB1R6(ruta.getB1R6());
        setNKS(ruta.getNKS());
        setB1R8(ruta.getB1R8());
//        setB1R9(ruta.getB1R9());

        //  blok2
        Blok2Dao blok2Dao = new Blok2DaoImpl();
        Blok2 blok2 = blok2Dao.getBlok2ByNKS(NKS);
        setB2R1(String.valueOf(blok2.getB2R1()));
        setB2R2(String.valueOf(blok2.getB2R2()));
        setB2R3(blok2.getB2R3());

        //blok3
        Blok3Dao blok3Dao = new Blok3DaoImpl();
        Blok3 blok3 = blok3Dao.getBlok3ByNKS(NKS);
        setB3R1(blok3.getB3R1());
        setB3R2a(blok3.getB3R2a());
        setB3R2b(blok3.getB3R2b());
        setB3R4(blok3.getB3R4());
        setB3R5a(blok3.getB3R5a());
        setB3R5b(blok3.getB3R5b());

    }

    //untuk new kuesinoner
    public void resetModel() {
        try {
            setB1R2("");
        } catch (EntityException ex) {
            fireOnChange(this);
        }
        try {
            setB1R3("");
        } catch (EntityException ex) {
            fireOnChange(this);
        }
        setNama_kec("");
        setNama_desa("");
        setB1R4("");
        setB1R5("");
        setB1R6("");
        setNKS("");
        setB1R8("");

        setB2R1("");
        setB2R2("");
        setB2R3("");

        setB3R1("");
        setNama_pencacah("");
        setB3R2a("");
        setB3R2b("03");
        setB3R4("");
        setNama_kortim("");
        setB3R5a("");
        setB3R5b("03");
    }

    //ini utk mendapatkan nama kecamatan sesuai dengan kode
    public void getValidasiB1R2() throws EntityException {
        KecamatanDao dao = new KecamatanDaoImpl();
        Kecamatan kecamatan = dao.getKecamatanByKodeKec(B1R2);
        setNama_kec(kecamatan.getNama_kec());
    }

    public void getValidasiB1R3() throws EntityException {
        DesaDao dao = new DesaDaoImpl();
        Desa desa = dao.getDesaByKodeDesa_KodeKec(B1R3, B1R2);
        setNama_desa(desa.getNama_desa());
    }

    public void getValidasiB1R4(String no_bs) throws EntityException {
        BlokSensusDaoImpl daoImpl = new BlokSensusDaoImpl();
        BlokSensus bs = daoImpl.getBlokSensusByKodekec_KodeDesa_NoBS(B1R2, B1R3, no_bs);
        setB1R4(bs.getNo_bs());
    }

    public void getValidasiB1R5(String no_urut_bs) throws EntityException {
        BlokSensusDao dao = new BlokSensusDaoImpl();
        BlokSensus bs = dao.getBlokSensusByKodekec_KodeDesa_NoUrutBS(B1R2, B1R3, no_urut_bs);
        setB1R5(bs.getNo_urut_bs());
    }
    
    public void getValidasiB3R1(String B3R1) throws EntityException{
        MahasiswaDao dao = new MahasiswaDaoImpl();
        Mahasiswa pencacah = dao.getMahasiswaByNim(B3R1);
        setNama_pencacah(pencacah.getNama());
        
        try {
            setKortimByPencacah();
        } catch (EntityException ex) {
            setNama_kortim(nama_pencacah);
            setB3R4(B3R1);
        }
    }

    public void getValidasiB3R4(String B3R4) throws EntityException{
//        PengawasanDao dao = new PengawasanlDaoImpl();
//        Pengawasan pengawasan = dao.getPengawasanByNim_Pencacah(B3R1);
//        setB3R4(pengawasan.getNim_kortim());
//        
        MahasiswaDao daoMhs = new MahasiswaDaoImpl();
        Mahasiswa kortim = daoMhs.getMahasiswaByNim(B3R4);
        setNama_kortim(kortim.getNama());
        
    }
    //otomatis saat nim pencacah diisi
    private void setKortimByPencacah() throws EntityException {
        PengawasanDao dao = new PengawasanlDaoImpl();

        //get nim kortim by nim pencacah, ambil dari tabel pengawasan
        Pengawasan pengawasan = dao.getPengawasanByNim_Pencacah(B3R1);
        setB3R4(pengawasan.getNim_kortim());

        //get nama kortim by nim kortim
        MahasiswaDao mahasiswaDao = new MahasiswaDaoImpl();
        Mahasiswa kortim = mahasiswaDao.getMahasiswaByNim(pengawasan.getNim_kortim());
        setNama_kortim(kortim.getNama());
    }
    
    /**
     * Digunakan untuk memberi tahukan kepada listener bahwa model berubah. View
     * akan otomatis berubah juga. panggil method ini disetiap setter class
     * model ini kecuali di setter listener
     * @param model this class
     */
    protected void fireOnChange(Blok123Model model) {
        if (listener != null) {
            listener.onChange(this);
        }
    }

    //getter
    public String getNama_kec() {
        return nama_kec;
    }

    public String getB1R2() {
        return B1R2;
    }

    public String getNama_desa() {
        return nama_desa;
    }

    public String getB1R3() {
        return B1R3;
    }

    public String getB1R4() {
        return B1R4;
    }

    public String getB1R5() {
        return B1R5;
    }

    public String getB1R6() {
        return B1R6;
    }

    public String getNKS() {
        return NKS;
    }

    public String getB1R8() {
        return B1R8;
    }

    public String getB2R1() {
        return B2R1;
    }

    public String getB2R2() {
        return B2R2;
    }

    public String getB2R3() {
        return B2R3;
    }

    public String getNama_pencacah() {
        return nama_pencacah;
    }

    public String getB3R1() {
        return B3R1;
    }

    public String getNama_kortim() {
        return nama_kortim;
    }

    public String getB3R4() {
        return B3R4;
    }

    public String getB3R2a() {
        return B3R2a;
    }

    public String getB3R2b() {
        return B3R2b;
    }

    public String getB3R5a() {
        return B3R5a;
    }

    public String getB3R5b() {
        return B3R5b;
    }

    public MahasiswaModel getMahasiswaModel() {
        return mahasiswaModel;
    }

    public Blok123Listener getListener() {
        return listener;
    }

    //setter
    public void setNama_kec(String nama_kec) {
        this.nama_kec = nama_kec;
        fireOnChange(this);
    }

    public void setB1R2(String B1R2) throws EntityException {
        this.B1R2 = B1R2;
        getValidasiB1R2();
        fireOnChange(this);
    }

    public void setNama_desa(String nama_desa) {
        this.nama_desa = nama_desa;
        fireOnChange(this);
    }

    public void setB1R3(String B1R3) throws EntityException {
        this.B1R3 = B1R3;
        getValidasiB1R3();
        fireOnChange(this);
    }

    public void setB1R4(String B1R4) {
        this.B1R4 = B1R4;
        fireOnChange(this);
    }

    public void setB1R5(String B1R5) {
        this.B1R5 = B1R5;
        fireOnChange(this);
    }

    public void setB1R6(String B1R6) {
        this.B1R6 = B1R6;
        fireOnChange(this);
    }

    public void setNKS(String NKS) {
        this.NKS = NKS;
        fireOnChange(this);
    }

    public void setB1R8(String B1R8) {
        this.B1R8 = B1R8;
        fireOnChange(this);
    }

    public void setB2R1(String B2R1) {
        this.B2R1 = B2R1;
        fireOnChange(this);
    }

    public void setB2R2(String B2R2) {
        this.B2R2 = B2R2;
        fireOnChange(this);
    }

    public void setB2R3(String B2R3) {
        this.B2R3 = B2R3;
        fireOnChange(this);
    }

    public void setNama_pencacah(String nama_pencacah) {
        this.nama_pencacah = nama_pencacah;
        fireOnChange(this);
    }

    public void setB3R1(String B3R1) {
        this.B3R1 = B3R1;
        fireOnChange(this);
    }

    public void setNama_kortim(String nama_kortim) {
        this.nama_kortim = nama_kortim;
        fireOnChange(this);
    }

    public void setB3R4(String B3R4) {
        this.B3R4 = B3R4;
        fireOnChange(this);
    }

    public void setB3R2a(String B3R2a) {
        this.B3R2a = B3R2a;
        fireOnChange(this);
    }

    public void setB3R2b(String B3R2b) {
        this.B3R2b = B3R2b;
        fireOnChange(this);
    }

    public void setB3R5a(String B3R5a) {
        this.B3R5a = B3R5a;
        fireOnChange(this);
    }

    public void setB3R5b(String B3R5b) {
        this.B3R5b = B3R5b;
        fireOnChange(this);
    }

    public void setListener(Blok123Listener listener) {
        this.listener = listener;
    }

    public void setMahasiswaModel(MahasiswaModel mahasiswaModel) {
        this.mahasiswaModel = mahasiswaModel;
        fireOnChange(this);
    }
}
